/**
*****************************************************************************
 * DESC ：创建新用户类. 
 * Author : Junwei Zhang 
 * CREATEDATE ：2012-6-3 
 * MODIFYLIST ：       	Name 			 Date 			Reason/Contents
 * 				------------------------------------------------------ 
 * 				 	Junwei Zhang     2012-6-3		Created.
 ****************************************************************************
**/
package com.me.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;

/**
 * 创建新用户类.
 * <br/> 下午3:56:31 2012-6-3
 * @Author <a href="mailto:mestudying@sina.com">Junwei Zhang</a><br/>
 **/
public class JTalkAccountCreater extends JDialog {
	private JTalkClientFront parent = null;
	private JPanel jPanelIcon = null;
	private JLabel jLableIcon = null;
	private JPanel jPanelCreateAccount = null;
	private JLabel jLableCreateAccount = null;
	private JLabel jLableUserName = null;
	private JTextField jTextFieldUserName = null;
	private JLabel jLablePassword = null;
	private JPasswordField jPasswordField = null;
	private JCheckBox jCheckBoxDefaultAccount = null;
	private JButton jButtonCreate = null;
	private JLabel jLableDisplayMsg = null;
	
	private static final long serialVersionUID = 1L;
	
	public JTalkAccountCreater(JTalkClientFront jTalkClient, boolean modal){
		super(jTalkClient, modal);
		this.parent = jTalkClient;
		this.initComponents();
		this.getRootPane().setDefaultButton(jButtonCreate);
		this.setLocationRelativeTo(jTalkClient);
		this.setResizable(false);
		this.setTitle("Create JTalk Account");
	}

	/**
	 * 初始化组件.
	 */
	private void initComponents() {
		// TODO Auto-generated method stub
		jPanelIcon = new JPanel();
		jLableIcon = new JLabel();
		jPanelCreateAccount = new JPanel();
		jLableUserName = new JLabel();
		jLablePassword = new JLabel();
		jTextFieldUserName = new JTextField();
		jPasswordField = new JPasswordField();
		jButtonCreate = new JButton();
		jCheckBoxDefaultAccount = new JCheckBox();
		jLableDisplayMsg = new JLabel();
		ClassLoader classLoader = JTalkAccountCreater.class.getClassLoader();
		
		jLableIcon.setIcon(new ImageIcon(classLoader.getResource("com/jtalker/rs/icons/new_account.jpg")));
		jLableIcon.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		jPanelIcon.add(jLableIcon);
		this.getContentPane().add(jPanelIcon, BorderLayout.WEST);
		
		jPanelCreateAccount.setLayout(null);
		jPanelCreateAccount.setPreferredSize(new Dimension(300, 195));
		// jPanelCreateAccount.setSize(300, 195);
		jLableCreateAccount = new JLabel();
		jLableCreateAccount.setFont(new Font("Dialog", 1, 18));
		jLableCreateAccount.setHorizontalAlignment(SwingConstants.CENTER);
		jLableCreateAccount.setText("Create Account");
		jPanelCreateAccount.add(jLableCreateAccount);
		jLableCreateAccount.setBounds(40, 10, 220, 25);
		
		jLableUserName.setHorizontalAlignment(SwingConstants.RIGHT);
		jLableUserName.setText("User Name:");
		jPanelCreateAccount.add(jLableUserName);
		jLableUserName.setBounds(10, 60, 110, 18);
		
		jLablePassword.setHorizontalAlignment(SwingConstants.RIGHT);
		jLablePassword.setText("Password:");
		jPanelCreateAccount.add(jLablePassword);
		jLablePassword.setBounds(10, 90, 110, 18);
		
		jTextFieldUserName.setToolTipText("A New User Name");
		jTextFieldUserName.setText(System.getProperty("user.name"));
		jPanelCreateAccount.add(jTextFieldUserName);
		jTextFieldUserName.setBounds(130, 60, 150, 22);
		
		jPasswordField.setToolTipText("Password");
		jPanelCreateAccount.add(jPasswordField);
		jPasswordField.setBounds(130, 90, 150, 22);
		
		jButtonCreate.setText("Create");
		jButtonCreate.setToolTipText("Connects to server and trys to create an account");
		jPanelCreateAccount.add(jButtonCreate);
		jButtonCreate.setBounds(190, 130, 88, 28);
		jButtonCreate.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				createUserAccount(e);
			}
		});
		
		jCheckBoxDefaultAccount.setText("Default Account");
		jCheckBoxDefaultAccount.setSelected(true);
		jCheckBoxDefaultAccount.setToolTipText("Writes the name and passwd to your JTalker config file");
		jPanelCreateAccount.add(jCheckBoxDefaultAccount);
		jCheckBoxDefaultAccount.setBounds(10, 130, 170, 26);
		
		jLableDisplayMsg.setHorizontalAlignment(SwingConstants.CENTER);
		jLableDisplayMsg.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		jPanelCreateAccount.add(jLableDisplayMsg);
		jLableDisplayMsg.setBounds(10, 170, 270, 20);
		
		this.getContentPane().add(jPanelCreateAccount, BorderLayout.CENTER);
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				closingDialog(e);
			}
		});
		
		this.pack();
	}
	/**
	 * 关闭对话框时触发的方法.
	 * @param e
	 */
	private void closingDialog(WindowEvent e) {
		this.setVisible(false);
		this.dispose();
	}
	/**
	 * 创建一个新的用户.
	 * @param e
	 */
	private void createUserAccount(ActionEvent e) {
		// TODO Auto-generated method stub
		this.jLableDisplayMsg.setText("Checking inputs...");
		String userName = this.jTextFieldUserName.getText();
		String password = new String(this.jPasswordField.getPassword());
		if(this.parent.isConnected){
			this.parent.connectServer(null);
		}
		boolean isSuccessful = this.parent.theClient.createNewAccount(userName, password);
		if(this.jCheckBoxDefaultAccount.isSelected()){
			this.parent.theClient.setUserName(userName);
			this.parent.theClient.setPassword(password);
		}
		if(isSuccessful){
			this.closingDialog(null);
		}
	}

}
